"""
检查MongoDB数据库结构
"""
import os
from pymongo import MongoClient

def main():
    # 连接MongoDB
    mongo_uri = os.getenv('MONGODB_URI', 'mongodb://admin:newhand@localhost:27017/')
    client = MongoClient(mongo_uri)

    print("=" * 60)
    print("MongoDB数据库检查")
    print("=" * 60)

    # 列出所有数据库
    print("\n所有数据库:")
    for db_name in client.list_database_names():
        print(f"  - {db_name}")

    # 检查常见的数据库
    target_dbs = ['crawler_db', 'hotspot_db', 'hot_radar_db', 'admin', 'test']

    for db_name in target_dbs:
        if db_name in client.list_database_names():
            print(f"\n数据库: {db_name}")
            print("=" * 60)
            db = client[db_name]

            # 列出所有集合
            collections = db.list_collection_names()
            if collections:
                print(f"集合列表 ({len(collections)}个):")
                for coll_name in collections:
                    coll = db[coll_name]
                    count = coll.count_documents({})
                    print(f"  - {coll_name}: {count} 条记录")

                    # 如果有数据，显示一个样例
                    if count > 0:
                        sample = coll.find_one()
                        print(f"    样例数据: {list(sample.keys())[:10]}")
            else:
                print("  (空数据库)")

    client.close()
    print("\n" + "=" * 60)

if __name__ == '__main__':
    main()
